﻿using System;
using System.Configuration;

namespace Framework.Common.Configs
{
    public static class SystemConfig
    {
        /// <summary>
        /// 获取AppSetting设置（忽略大小写）
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetString(string key)
        {
            if (key.IsBlank()) return string.Empty;
            return ConfigurationManager.AppSettings[key] ??
                   ConfigurationManager.AppSettings[key.ToLower()] ?? 
                   ConfigurationManager.AppSettings[key.ToUpper()];
        }

        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings[key];
            if (setting != null)
            {
                return setting.ConnectionString;
            }
            return null;
        }

        /// <summary>
        /// 获取Int
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static int GetInt(string key, int defValue = 0)
        {
            return GetString(key).StrToInt32(defValue);
        }

        /// <summary>
        /// 获取long
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static long GetLong(string key, long defValue = 0)
        {
            return GetString(key).StrToInt64(defValue);
        }

        /// <summary>
        /// 获取byte
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static Byte GetByte(string key, byte defValue = 0)
        {
            return GetString(key).StrToByte(defValue);
        }

        /// <summary>
        /// 获取 datetime
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(string key)
        {
            return GetString(key).StrToDateTime();
        }

        /// <summary>
        /// 获取 bool
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static Boolean GetBoolean(string key, bool defValue = false)
        {
            return GetString(key).StrToBoolean(defValue);
        }
    }
}
